<?php
namespace App\Http\Libs;
use \Cache;
class Helper_Huanxin {
	
	static $apiurl = "https://a1.easemob.com/tyrbl/wjtrdeveloper/";
	
	/**
	 * 更新token
	 */
	static function getToken() {
		global $root_dir;
		$policy = array(
			'cache_dir' => $root_dir . "/tmp/cache",
			'life_time' => 604790
		);
		$access_token =Cache::get('token');
		if (!empty($access_token)) {
			$url = self::$apiurl . "token";
			$params = '{"grant_type":"client_credentials","client_id":"YXA6Z7k6cDTcEeWFnfnu7glbXg","client_secret":"YXA6KlysDqMyhDf68ptCNo8j6GKrNz4"}';
				
			$result = self::callRestfulApi($url, "POST", $params, "");
			$result_json = json_decode($result);
			$access_token = $result_json->{'access_token'};
			Cache::add('token', $access_token,$policy['life_time']);
		}
		return $access_token;
	}


	/**
	 * 发送文本消息给一组用户
	 * @param unknown_type $usernames 环信用户id数组
	 * @param unknown_type $msg 消息内容
	 * @param unknown_type $from 发送方环信id，默认admin为无界投融官方账号id
	 * @param array $ext 扩展参数
	 * @return mixed
	 */
	static function sendMessage($usernames,$msg,$from="admin",$ext=array("type"=>"txt")) {
		
		$url = self::$apiurl . "messages";
		$params = array("target_type"=>"users",
				"target"=>$usernames,
				"msg"=>array("type"=>"txt",
						"msg"=>$msg),
				"from"=>$from,
				"ext"=>$ext
				);
		
		$headers=array('Authorization: Bearer ' . self::getToken());
		$result = self::callRestfulApi($url, "POST", json_encode($params), $headers);
		return $result;
	}
	
	/**
	 * 创建群组
	 * @param unknown_type $groupname 群组名称, 此属性为必须的{"name":"xxx","type":"capital","project_id":XXX}
	 * @param unknown_type $desc 群组描述, 此属性为必须的;{"type":"capital","project_id":XXX}
	 * @param unknown_type $owner 群组的管理员, 此属性为必须的
	 * @param unknown_type $members 群组成员,此属性为可选的,但是如果加了此项,数组元素至少一个（注：群主jma1不需要写入到members里面）
	 * @param unknown_type $public 是否是公开群, 此属性为必须的,为false时为私有群
	 * @param unknown_type $maxusers 群组成员最大数(包括群主), 值为数值类型,默认值200,此属性为可选的
	 * @param unknown_type $approval 加入公开群是否需要批准, 默认值是true（加群需要群主批准）, 此属性为可选的,只作用于公开群
	 */
	static function createGroup($groupname, $desc, $owner, $members='', $public=true, $maxusers=200, $approval=true) {
		$url = self::$apiurl . "chatgroups";
		$params = array("groupname"=>$groupname,
				"desc"=>$desc,
				"public"=>$public,
				"maxusers"=>$maxusers,
				"approval"=>$approval,
				"owner"=>$owner
		);
		if ($members) {
			$params['members'] = $members;
		}
		$headers=array('Authorization: Bearer ' . self::getToken());
		$result = self::callRestfulApi($url, "POST", json_encode($params), $headers);
		return $result;
	}
	
	/**
	 * 修改群信息
	 * @param unknown_type $group_id 群id
	 * @param array $params 要修改的群属性;  "{"需用"%7B"代替
	 * 						array(
	 "groupname"=>"testrestgrp12", //群组名称
	 "description"=>"update groupinfo", //群组描述
	 "maxusers"=>300, //群组成员最大数(包括群主), 值为数值类型
	 )
	 * @return mixed
	 */
	static function editGroup($group_id, $params) {
		if (!isset($group_id) || $group_id == "") return '{"error":"need a group_id"}';
		if (!isset($params) || !is_array($params) || !count($params)) return '{"error":"need edit params"}';
		$url = self::$apiurl . "chatgroups/" . $group_id;
		$headers=array('Authorization: Bearer ' . self::getToken());
		$result = self::callRestfulApi($url, "PUT", json_encode($params), $headers);
		return $result;
	}
	
	/**
	 * 删除群组
	 * @param unknown_type $groupid 群id
	 * @return mixed
	 */
	static function deleteGroup($groupid) {
		$url = self::$apiurl . "chatgroups/" . $groupid;
		$headers=array('Authorization: Bearer ' . self::getToken());
		$result = self::callRestfulApi($url, "DELETE", "", $headers);
		return $result;
	}
	
	/**
	 * 加入群聊
	 * @param unknown_type $groupid 群id
	 * @param unknown_type $username 要加入用户环信id
	 * @return mixed
	 */
	static function addMember($groupid, $username) {
		$url = self::$apiurl . "chatgroups/".$groupid."/users/" . $username;
		$headers=array('Authorization: Bearer ' . self::getToken());
		$result = self::callRestfulApi($url, "POST", "{}", $headers);
		return $result;
	}
	
	/**
	 * 群组减人
	 * @param unknown_type $groupid 群id
	 * @param unknown_type $username 要加入用户环信id
	 * @return mixed
	 */
	static function deleteMember($groupid, $username) {
		$url = self::$apiurl . "chatgroups/".$groupid."/users/" . $username;
		$headers=array('Authorization: Bearer ' . self::getToken());
		$result = self::callRestfulApi($url, "DELETE", "{}", $headers);
		return $result;
	}
	
	/**
	 * 获得好友列表
	 * @param unknown_type $username 用户环信id
	 * @param unknown_type $return_type 返回类型， json，array
	 * @return unknown
	 */
	static function getFriendList($username, $return_type) {
		if (!isset($username) || $username == "") return "";
		$url = self::$apiurl . "users/".$username."/contacts/users";
		$headers=array('Authorization: Bearer ' . self::getToken());
		$result = self::callRestfulApi($url, "GET", "", $headers);
		$result2 = json_decode($result);
		switch ($return_type){
			case("json") : return $result2->{'duration'} == 0 ? "" : json_encode($result2->{'data'});
			case("array") : return $result2->{'duration'} == 0 ? "" : $result2->{'data'};
		}
		
	}
	
	/**
	 * 是否好友
	 * @param unknown_type $username 用户环信id
	 * @param unknown_type $friend_username 用户好友环信id
	 * @return string
	 */
	static function isFriend($username, $friend_username) {
		if (!isset($username) || $username == "") return "notlogin";
		if ($username == $friend_username) return "self";
		$friend_list = self::getFriendList($username, "array");
		if($friend_list && in_array($friend_username, $friend_list)) {
			return "friend";
		} else {
			return "stranger";
		}
	}
	
	/**
	 * 批量判断是否好友
	 * @param unknown_type $username 用户环信id
	 * @param array $friend_usernames 用户好友环信id，
	 * @return string
	 */
	static function isFriends($username, $friend_usernames) {
		if (!is_array($friend_usernames)) return "error type";
		if (!isset($username) || $username == "") return "nologin";
		$result = array();
		$friend_list = self::getFriendList($username, "array");
		foreach ($friend_usernames as $k=>$friend_username) {
			$result[$k]['usernames'] =$friend_username;
			if ($username == $friend_username) {
				$result[$k]['relation'] = "slef";
			}else if($friend_list && in_array($friend_username, $friend_list)) {
				$result[$k]['relation'] = "friend";
			} else {
				$result[$k]['relation'] = "stranger";
			}
		}
		return json_encode($result);
	}
	
	/**
	 * 发送好友请求
	 * @param unknown_type $username 用户环信id
	 * @param unknown_type $friend_username 用户好友环信id
	 * @return mixed
	 */
	static function requestFriend($username, $friend_username) {
		$url = self::$apiurl . "users/".$username."/contacts/users/".$friend_username;
		$headers=array('Authorization: Bearer ' . self::getToken());
		$result = self::callRestfulApi($url, "POST", "{}", $headers);
		return $result;
	}
	
	/**
	 * 注册环信用户
	 * @param unknown_type $users array("username"=>username,"password"=>username) 密码为用户名，批量注册则传数组
	 * @return mixed
	 */
	static function register($users) {
		$url = self::$apiurl . "users";
		$headers=array('Authorization: Bearer ' . self::getToken());
		$result = self::callRestfulApi($url, "POST", json_encode($users), $headers);
		return $result;
	}

    /**
     * @param"name":"testchatroom", //聊天室名称, 此属性为必须的
     * @param"description":"server create chatroom", //聊天室描述, 此属性为必须的
     * @param"maxusers":300, //聊天室成员最大数(包括群主), 值为数值类型,默认值200,此属性为可选的
     * @param"owner":"jma1", //聊天室的管理员, 此属性为必须的
     * @param"members":["jma2","jma3"] //聊天室成员,此属性为可选的,但是如果加了此项,数组元素至少一个（注：群主jma1不需要写入到members里面）
     */
    static function createChatroom($roomname, $desc, $owner, $members='', $maxusers=200){
        $url = self::$apiurl . "chatrooms";
        $params = array("name"=>$roomname,
                        "description"=>$desc,
                        "maxusers"=>$maxusers,
                        "owner"=>$owner,
        );
        if ($members) {
            $params['members'] = $members;
        }
        $headers=array('Authorization: Bearer ' . self::getToken());
        $result = self::callRestfulApi($url, "POST", json_encode($params), $headers);
        return $result;
    }



    /**
     * 聊天室减人
     * @param unknown_type $groupid 聊天室id
     * @param unknown_type $username 要加入用户环信id
     * @return mixed
     */
    static function deleteRoomMember($roomid, $username) {
        $url = self::$apiurl . "chatrooms/".$roomid."/users/" . $username;
        $headers=array('Authorization: Bearer ' . self::getToken());
        $result = self::callRestfulApi($url, "DELETE", "{}", $headers);
        return $result;
    }

    /**
     * 聊天室加人
     * @param unknown_type $groupid 聊天室id
     * @param unknown_type $username 要加入用户环信id
     * @return mixed
     */
    static function addRoomMember($room_id, $username) {
        $url = self::$apiurl . "chatrooms/".$room_id."/users/" . $username;
        $headers=array('Authorization: Bearer ' . self::getToken());
        $result = self::callRestfulApi($url, "POST", "{}", $headers);
        return $result;
    }


    /**
     * 删除聊天室
     * @param unknown_type $groupid 聊天室id
     * @return mixed
     */
    static function deleteRoom($roomid) {
        $url = self::$apiurl . "chatrooms/" . $roomid;
        $headers=array('Authorization: Bearer ' . self::getToken());
        $result = self::callRestfulApi($url, "DELETE", "", $headers);
        return $result;
    }



    /**
     * 修改聊天室信息
     * 名称和描述不能有空格，否则无法更新
     * @return mixed
     */
    static function editRoom($room_id, $params) {
        if (!isset($room_id) || $room_id == "") return '{"error":"need a room_id"}';
        if (!isset($params) || !is_array($params) || !count($params)) return '{"error":"need edit params"}';
        $url = self::$apiurl . "chatrooms/" . $room_id;
        $headers=array('Authorization: Bearer ' . self::getToken());
        $result = self::callRestfulApi($url, "PUT", json_encode($params), $headers);
        return $result;
    }

    /**
     *  获取一个app里面所有的聊天室
     * @return mixed
     */
    static function getRooms() {
        $url = self::$apiurl . "chatrooms" ;
        $headers=array('Authorization: Bearer ' . self::getToken());
        $result = self::callRestfulApi($url, "GET", "{}", $headers);
        return $result;
    }


    /**
     *  获取一个聊天室详情
     * @return mixed
     */
    static function getRoom($room_id) {
        $url = self::$apiurl . "chatrooms/" . $room_id;
        $headers=array('Authorization: Bearer ' . self::getToken());
        $result = self::callRestfulApi($url, "GET", "{}", $headers);
        return $result;
    }


    /**
     *  获取一个app下全部的会员
     * @return mixed
     */
    static function getUsers() {
        $url = self::$apiurl . "users";
        $headers=array('Authorization: Bearer ' . self::getToken());
        $result = self::callRestfulApi($url, "GET", "{}", $headers);
        return $result;
    }

    /**
     *  获取单个会员
     * @return mixed
     */
    static function getUser($username) {
        $url = self::$apiurl . "users/".$username;
        $headers=array('Authorization: Bearer ' . self::getToken());
        $result = self::callRestfulApi($url, "GET", "{}", $headers);
        return $result;
    }

    /**
	 * 调用restful接口
	 * @param unknown_type $URL 接口地址
	 * @param unknown_type $type GET、POST、PUT、DELETE
	 * @param unknown_type $params 参数，JSON字符串格式
	 * @param unknown_type $headers header参数
	 * @param unknown_type $user_password 用户名和密码[username]:[password]
	 * @return mixed
	 */
	static function callRestfulApi($url,$type,$params,$headers,$user_password=""){
		$ch = curl_init();
		$timeout = 5;
		curl_setopt ($ch, CURLOPT_URL, $url);
		if($headers!=""){
			curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
		}else {
			curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type: text/json'));
		}
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	
//         CURLOPT_USERPWD 传递一个连接中需要的用户名和密码，格式为："[username]:[password]"。
		if($user_password!="")
			curl_setopt ($ch, CURLOPT_USERPWD, $user_password);
	
		switch ($type){
			case "GET" : curl_setopt($ch, CURLOPT_HTTPGET, true);break;
			case "POST": curl_setopt($ch, CURLOPT_POST,true);
			curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;
			case "PUT" : curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
			curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;
			case "DELETE":curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
			curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;
		}
		$file_contents = curl_exec($ch);//获得返回值
		curl_close($ch);
		return $file_contents;
	}

}